1
'***************************** Module Header ******************************\
2 '* Module Name: RoomTalker.svc.vb
3 '* Project: VBASPNETAJAXWebChat
4 '* Copyright (c) Microsoft Corporation
6 '* The project illustrates how to design a simple AJAX web chat application.
7 '* We use jQuery, ASP.NET AJAX at client side and Linq to SQL at server side.
8 '* In this sample, we could create a chat room and invite someone
9 '* else to join in the room and start to chat.
11 '* In this file, we create an Ajax-enabled WCF service which used to be called
12 '* from the client side.
14 '* This source is subject to the Microsoft Public License.
15 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
16 '* All other rights reserved.
18 '\****************************************************************************
20 Imports System
.ServiceModel
21 Imports System
.ServiceModel
.Activation
22 Imports System
.ServiceModel
.Web
24 <ServiceContract([Namespace
]:="http://VBASPNETAJAXWebChat",
25 SessionMode
:=SessionMode
.Allowed
)> _
26 <AspNetCompatibilityRequirements(
27 RequirementsMode
:=AspNetCompatibilityRequirementsMode
.Allowed
)> _
28 Public Class Transition
30 <OperationContract()> _
31 Public Sub CreateChatRoom(ByVal useralias
As String,
32 ByVal roomName
As String,
33 ByVal password
As String,
34 ByVal maxUser
As Integer,
35 ByVal needPassword
As Boolean)
41 Dim roomid
As Guid
= ChatManager
.CreateChatRoom(roomName
,
49 <OperationContract()> _
50 Public Function JoinChatRoom(ByVal roomid
As String,
51 ByVal [alias] As String) As ChatRoom
53 If Guid
.TryParse(roomid
, rid
) Then
54 ChatManager
.JoinChatRoom(rid
, HttpContext
.Current
, [alias])
56 Return New ChatRoom(rid
)
63 <OperationContract()> _
64 Public Sub LeaveChatRoom(ByVal roomid
As String)
65 If roomid Is
Nothing Then
66 roomid
= GetGUIDFromQuery(
67 HttpContext
.Current
.Request
.UrlReferrer
.Query
).ToString()
70 If Guid
.TryParse(roomid
, rid
) Then
71 ChatManager
.LeaveChatRoom(rid
, HttpContext
.Current
)
77 <OperationContract()> _
78 Public Function GetChatRoomList() As List(Of ChatRoom
)
79 Dim list
As List(Of tblChatRoom
) = ChatManager
.GetChatRoomList()
80 Dim result
As New List(Of ChatRoom
)()
81 For Each room
As tblChatRoom
In list
82 result
.Add(New ChatRoom(room
.ChatRoomID
))
87 <OperationContract()> _
88 Public Function GetChatRoomInfo(ByVal RoomID
As String) As ChatRoom
90 If Guid
.TryParse(RoomID
, rim
) Then
91 Return New ChatRoom(rim
)
98 <OperationContract()> _
99 Public Function GetRoomTalkerList() As List(Of RoomTalker
)
101 Dim result
As New List(Of RoomTalker
)()
102 Dim roomid
As Guid
= GetGUIDFromQuery(
103 HttpContext
.Current
.Request
.UrlReferrer
.Query
)
104 If roomid
<> Guid
.Empty
Then
105 Dim talkerList
As List(Of tblTalker
) =
106 ChatManager
.GetRoomTalkerList(roomid
)
107 For Each talker
As tblTalker
In talkerList
108 result
.Add(New RoomTalker(talker
, HttpContext
.Current
))
115 <OperationContract()> _
116 Public Function SendMessage(ByVal message
As String) As Boolean
117 Dim roomid
As Guid
= GetGUIDFromQuery(
118 HttpContext
.Current
.Request
.UrlReferrer
.Query
)
120 If roomid
<> Guid
.Empty
Then
121 Dim talker
As tblTalker
=
122 ChatManager
.FindTalker(roomid
, HttpContext
.Current
)
123 ChatManager
.SendMessage(talker
, message
)
131 <OperationContract()> _
132 Public Function RecieveMessage() As List(Of Message
)
133 Dim result
As New List(Of Message
)()
134 Dim roomid
As Guid
= GetGUIDFromQuery(
135 HttpContext
.Current
.Request
.UrlReferrer
.Query
)
137 If roomid
<> Guid
.Empty
Then
138 Dim messageList
As List(Of tblMessagePool
) =
139 ChatManager
.RecieveMessage(ChatManager
.GetChatRoom(roomid
))
141 For Each msg
As tblMessagePool
In messageList
142 result
.Add(New Message(msg
, HttpContext
.Current
))
149 Private Function GetGUIDFromQuery(ByVal query
As String) As Guid
151 If String.IsNullOrEmpty(query
) Then
155 Dim reg
As New Regex(
156 "i=([0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})")
157 Dim gid
As String = reg
.Match(query
).Groups(1).Value
158 If Guid
.TryParse(gid
, rim
) Then